﻿
//添加cookie
//参数列表:名, 值, 有效期(单位:小时)
function AddCookie(name, value, exitHours)
{
    var cookie = name + "=" + escape(value);
    //如果有效期大于0, 则设置时间, 传另外的参数则取默认, 即关闭浏览器即失效
    if (exitHours > 0)
    {
        var date = new Date();
        date.setTime(date.getTime() + exitHours * 3600 * 1000);
        cookie = cookie + ";expires=" + date.toGMTString();
    }
    document.cookie = cookie;
}

//获取cookie值
function GetCookieByName(name)
{
    var cookieArr = document.cookie.split('; ');
    for (var i = 0 ; i < cookieArr.length; i++)
    {
        var arr = cookieArr[i].split('=');
        if (arr[0] == name)
            return arr[1];
    }
    return "notFound";
}

//设置指定cookies过期
function DelCookieByName(name)
{
    var date = new Date();
    date.setTime(date.getTime() - 1000);
    document.cookie = name + "=whatever;expires=" + date.toGMTString();
}

//判断是否已登录
function IsLogin()
{
    if (GetCookieByName("Id") == "notFound")
        return false;
    else
        return true;
}